// 第一个参数需要规划线路的点，第二个参数线路规划的一些选项（如工具（car）、线路选择(最短)），第三个参数规划出的线路的样式
// points参数：[[lat1,lng1],[lat2,lng2]] 或者 [{lat: lat1,lng: lng2},{lat: lat2,lng: lng2}] (可以多个点)
// planOptions参数：
// {
//  vehicle: foot | car | bike , 默认bike
//  weighting: fastest | shortest, 默认fastest
// }
var RouteUtil = {
    plan: function (points, planOptions, lineOptions) {

        if (!points) {
            return;
        }

        for (var i = 0; i < points.length; i++) {
            var point = points[i];
            console.log(point)
            if (point.lat) {
                point.lon = point.lng;
                continue;
            } else {
                point.lat = point[0];
                point.lng = point[1];
                point.lon = point[1];
            }
        }

        var data = JSON.stringify({
            points: JSON.stringify(points),
            hopper: planOptions
        });

        // 线路规划服务的请求
        var routPalnUrl = localhostPath + "/routePlan/plan";
        // var routPalnUrl = "http://61.145.229.217:9091/routePlan/plan";
        // $.post(routPalnUrl,data,function (result) {
        //             var polyline = new L.polyline(result.gpx, lineOptions);
        //             return polyline;
        // })
        // 发起请求
        var polyline;
        $.ajax({
            url: routPalnUrl,
            contentType: "application/json",
            type: "POST",
            data: data,
            async: false,
            success: function (result) {
                if (result.gpx) {
                    polyline = L.polyline(result.gpx, lineOptions);
                }else {
                    polyline = L.polyline(points,lineOptions)
                }
                console.log("route plan err : "+result.msg);
            },
            error: function (msg) {
                console.log("route plan err : "+msg);
                // alert('请求失败，' + 'Error code: ' + msg);
                polyline = L.polyline(points,lineOptions);
            }
        });
        return polyline;
    },
    carPlan: function (points, lineOptions) {
        var polyline = RouteUtil.plan(points, {vehicle: 'car', weighting: 'fastest'}, lineOptions);
        console.log(polyline)
        return polyline;
    },
    bikePlan: function (points, lineOptions) {
        var polyline = RouteUtil.plan(points, {vehicle: 'bike', weighting: 'fastest'}, lineOptions);
        return polyline;
    },
    footPlan: function (points, lineOptions) {
        var polyline = RouteUtil.plan(points, {vehicle: 'foot', weighting: 'fastest'}, lineOptions);
        return polyline;
    },
    // 定位服务
    getNowLocation: function (userNum) {
        //  stayLocation : {userNum: '',lat: '',lng:'',stayTime: '',address； '',satyTime:''}
        // var getNowLocationUrl = "http://61.145.229.217:9091/location/getNow";
        var getNowLocationUrl = localhostPath + "/location/getNow";
        var nowStayLocation;
        var data = JSON.stringify({
            userNum: userNum,
        });
        $.ajax({
            url: getNowLocationUrl,
            contentType: "application/json",
            type: "POST",
            data: data,
            async: false,
            success: function (result) {
                nowStayLocation = result;
            },
            error: function (msg) {
                alert('请求失败，' + 'Error code: ' + msg);
            }
        });
        console.log(nowStayLocation);
        return nowStayLocation;
    },
    // 历史位置
    getStayLocations: function (userNum, startTime, stopTime) {
        //  stayLocation : {userNum: '',lat: '',lng:'',stayTime: '',address； '',satyTime:''}
        // var getStayLocationsUrl = "http://61.145.229.217:9091/location/getTracksByStay";
        var getStayLocationsUrl = localhostPath + "/location/getTracksByStay";
        var stayLocations;
        var data = JSON.stringify({
            userNum: userNum,
            startTime: startTime,
            stopTime: stopTime
        });
        $.ajax({
            url: getStayLocationsUrl,
            contentType: "application/json",
            type: "POST",
            data: data,
            async: false,
            success: function (result) {
                stayLocations = result;
            },
            error: function (msg) {
                alert('请求失败，' + 'Error code: ' + msg);
            }
        });
        return stayLocations;
    }
}